<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { luckDrawApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  const formLabelWidth = '160px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:'',
  })
  interface formQuert {
    pool_id:number
    less_nums:string
    gift_ids:string
  }
  let form = reactive({} as formQuert)
  const handleSubmit = ()=>{
    dataForm.value.validate((valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      luckDrawApi.wishWheelPoolSetGiftAdjust(form).then(()=>{
        loading.close()
        ToastSuccess('礼物调整成功')
        $emit('updateList')
        closeDialog()
      }).catch(()=>{
        loading.close()
      })
    })
  }
  const openDialog = (query:any={})=> {
    const { id, pool_name='', gift_adjust={} } = query,
    { less_nums='', gift_ids='' } = gift_adjust || {}
    Object.assign(form,{
      pool_id:id,
      less_nums,
      gift_ids
    })
    action.title = `礼物池名称：${pool_name} -礼物调整`
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="40%"
      :title="action.title">
      <el-form ref="dataForm" label-position="top" :model="form">
        <el-form-item :label-width="formLabelWidth" label="礼物池礼物剩余数量：">
          <el-input
            type="textarea"
            v-model="form.less_nums"
            :autosize="{ minRows: 3, maxRows: 3}"
            placeholder="剩余数量最少为1不可为0，多个礼物剩余数量用英文,隔开"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="指定礼物ID：">
          <el-input
            type="textarea"
            v-model="form.gift_ids"
            :autosize="{ minRows: 3, maxRows: 3}"
            placeholder="多个礼物ID用英文,隔开"
          />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
